<template>
	<view class="page-box">
		<view class="page-header">
			<view class="left-icon">
				<u-icon name="list" color="#fff" size="26" @click="openLeft"></u-icon>
			</view>
			<view class="tab-list">
				<view :class="index === checkIndex ? 'tab-item active' : 'tab-item'" v-for="(item, index) in tabList"
					:key="item.name" @click="changeTabs(index)">
					{{item.name}}
				</view>
			</view>
			<view class="num-box">
				<view>
					<u--text text="账户余额" color="#fff"></u--text>
				</view>
				<view class="money">
					<u--text mode="price" :text="totalIncome - totalExpense" size="28" color="#fff"></u--text>
				</view>
				<view class="month-money">
					<u--text text="总支出" size="12" color="#fff"></u--text>
					<u--text mode="price" size="12" :text="totalExpense" color="#fff"></u--text>
				</view>
			</view>
		</view>
		<view class="page-content">
			<u-sticky offset="20">
				<view class="overview">
					<view class="name">
						<u--text text="概况" size="16" color="#4f5a7d"></u--text>
					</view>
					<view class="see-more" @click="seeAll">
						<u--text text="查看全部" size="14" color="#767a7f" style="padding-right: 10rpx"></u--text>
						<u-icon name="arrow-right" color="#767a7f" size="14"></u-icon>
					</view>
				</view>
			</u-sticky>
			<view class="list">
			  <u-list v-if="billList.length > 0" style="height: 100%;" @scrolltolower="scrolltolower" :pagingEnabled="true">
				<view class="day-item" v-for="item in billList" :key="item.id">
					<view class="item-header">
						<view class="date">{{item.day | dayFormat}}&nbsp;&nbsp;{{item.week | weekFormat}}</view>
						<view class="total">
							<u-tag text="出" bgColor="#8f94fb" borderColor="#8f94fb" size="mini"></u-tag><text class="item-total-money">{{item.day_pay | fixed2 }}</text>
							<u-tag text="入"  bgColor="#8f94fb" borderColor="#8f94fb" size="mini"></u-tag><text class="item-total-money">{{item.day_income | fixed2}}</text>
						</view>
					</view>
					<view class="bill-item" v-for="sitem in item.bills" :key="sitem.id" @click="seeDetail(sitem)">
						<view class="icon">
							<view style="color: #697AE4"
								:class="sitem.icon ? `iconfont ${sitem.icon}` : 'iconfont icon-listgouwu'"></view>
						</view>
						<view class="category">{{sitem.category_name}}</view>
						<view :class="sitem.flag === 'pay' ? 'pay' : 'income'">
							{{sitem.flag === 'pay' ? '-' : '+'}}
							{{(sitem.cost / 100).toFixed(2)}}
						</view>
					</view>
				</view> 
			  </u-list>
			  <view v-else>
				  <u-empty mode="list">
				</u-empty>
			  </view>
			</view>
		</view>
		<view class="footer">
			<u-button iconColor="#fff" size="large" icon="plus" @click="toAdd" :color="bgColor"></u-button>
		</view>
		<left-box v-if="showLeft" @fetchBillList="fetchBillList" @closeLeft="closeLeft" />
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	import LeftBox from '../components/left-box.vue'
	import {
		getBillList
	} from '@/api/bill.js'
	export default {
		components: {
			LeftBox
		},
		data() {
			return {
				bgColor: 'linear-gradient(to right, #8f94fb, #4e54c8)',
				tabList: [{
						name: '流水'
					},
					{
						name: '统计'
					},
				],
				checkIndex: 0,
				showLeft: false,
				totalExpense: 0,
				totalIncome: 0,
				searchConf: {
					pageNo: 1,
					pageSize: 5,
					billDate: uni.$u.timeFormat(+new Date(), 'yyyy-mm')
				},
				billList: []
			}
		},
		computed: {
			...mapGetters(['userInfo'])
		},
		onShow() {
			this.fetchBillList()						
		},
		methods: {
			scrolltolower() {
				this.loadMore()
			},
			async fetchBillList() {
				this.searchConf.pageNo = 1
				this.billList = []
				const rsp = await getBillList(this.searchConf)
				if (rsp.code === 0) {
					this.billList = rsp.data.list 
					this.totalExpense = rsp.data.totalExpense
					this.totalIncome = rsp.data.totalIncome
				}
			},
			async loadMore() {
				this.searchConf.pageNo ++
				const rsp = await getBillList(this.searchConf)
				if (rsp.code === 0) {
					this.billList = this.billList.concat(rsp.data.list) 
					this.totalExpense = rsp.data.totalExpense
					this.totalIncome = rsp.data.totalIncome
				}
			},
			changeTabs(index) {
				if (index === 1) {
					uni.redirectTo({
						url: '/pages/statistics/index'
					});
				}
			},
			openLeft() {
				this.showLeft = true
			},
			closeLeft() {
				this.showLeft = false
			},
			toAdd() {
				uni.navigateTo({
					url: '/pages/add/index'
				});
			},
			seeAll() {
				// uni.showToast({
				// 	title: '此功能暂未开放',
				// 	icon:'none'
				// })
				uni.navigateTo({
					url: '/pages/all-bill/index'
				});
			},
			seeDetail(item) {
				uni.$u.route('/pages/bill-detail/index', item);
			}
		}
	}
</script>
<style lang="scss">
	.u-sticky {
		top: 0 !important;
	}

	.footer {
		.u-button--large {
			height: 65px;
		}
	}
</style>
<style lang="scss" scoped>
	.page-box {
		display: flex;
		flex-direction: column;
		overflow: hidden;
		height: 100vh;

		.page-header {
			width: 100%;
			height: 450rpx;
			background: -webkit-linear-gradient(to right, #8f94fb, #4e54c8);
			/* Chrome 10-25, Safari 5.1-6 */
			background: linear-gradient(to right, #8f94fb, #4e54c8);
			/* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
			position: relative;

			.left-icon {
				padding: 110rpx 0 0 50rpx;
			}

			.tab-list {
				position: absolute;
				top: 90rpx;
				left: 50%;
				transform: translate(-50%, 0);
				display: flex;
				align-items: center;
				justify-content: center;

				.tab-item {
					color: #9BA8E8;
					font-size: 32rpx;
					padding: 0 60rpx;
				}

				.active {
					margin-top: 15rpx;
					color: #fff;
					font-weight: 500;

					&::after {
						content: '';
						display: block;
						margin-top: 15rpx;
						background: #fff;
						width: 100%;
						height: 3px;
						border-radius: 2px;
					}
				}
			}

			.num-box {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				margin-top: 60rpx;

				.money {
					padding: 10rpx 50rpx;
					border-bottom: 0.5px solid #f5f5f5;
				}

				.month-money {
					display: flex;
					justify-content: center;
					align-items: center;
					padding-top: 15rpx;
				}
			}

		}

		.page-content {
			flex: 1;
			overflow: auto;

			.overview {
				display: flex;
				justify-content: space-between;
				align-items: center;
				background-color: #fff;
				padding: 20rpx;

				.see-more {
					display: flex;
					justify-content: space-between;
					align-items: center;
				}
			}

			.list {
				padding: 0 20rpx 20rpx 20rpx;

				.day-item {
					.item-header {
						display: flex;
						align-items: center;
						justify-content: space-between;
						.total {
							display: flex;
							align-items: center;
							.item-total-money {
								color: #666;
								font-size: 14px;
								padding: 0 10rpx;
							}
						}
						.date {
							color: #767a7f;
							padding-bottom: 20rpx;
						}

					}

					.bill-item {
						display: flex;
						justify-content: space-around;
						align-items: center;
						background-color: rgba(210, 238, 247, .2);
						padding: 25rpx 20rpx;
						margin-bottom: 30rpx;
						border-radius: 8rpx;

						.category {
							flex: 1;
							font-weight: 500;
							color: #4f5a7d;
							margin-left: 10rpx;

						}

						.pay {
							color: #c8223b;
							font-weight: 500;
						}

						.income {
							color: #2f307d;
							font-weight: 500;
						}
					}
				}
			}
		}

		.footer {
			width: 100%;
		}
	}
</style>
